package cn.edu.scau.cmi.linjinyong.multiton;

import cn.edu.scau.cmi.linjinyong.singleton.Data;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;

import java.util.List;

public class MultitonController {
    @FXML
    private TextField marshalNameField;
    @FXML
    private TableView<Data> marshalTable;
    @FXML
    private TableColumn<Data, String> nameColumn;
    @FXML
    private TableColumn<Data, String> objectColumn;
    @FXML
    private Label result;

    private ObservableList<Data> marshalDatas;

    @FXML
    private void initialize() {
        marshalDatas = FXCollections.observableArrayList();
        nameColumn.setCellValueFactory(new PropertyValueFactory("name"));
        objectColumn.setCellValueFactory(new PropertyValueFactory("object"));
        marshalTable.setItems(marshalDatas);
    }

    @FXML
    private void createMarshal() {
        if (marshalNameField.getText() != null && !"".equals(marshalNameField.getText())) {
            Marshal marshal = Marshal.createMarshal(marshalNameField.getText());
            if(marshal!=null){
                marshalDatas.add(new Data(marshal.getName(), marshal.toString()));
                result.setText("创建成功");
            }else {
                result.setText("已达最大个数");
            }
            marshalTable.setItems(marshalDatas);
        }else {
            result.setText("名字不能为空");
        }
    }
}
